package com.example.sunming.warning;


import com.example.sunming.utils.Java8DateUtils;

import java.util.Calendar;
import java.util.Date;

/**
 * @description: 解析mdrs预报开始时间和结束时间
 * @date: 2020/12/9
 * @author: sunming
 */
public class ParseMdrsTimeUtil {

    private static String timePatternOne = "\\d{2}日\\d{2}:\\d{2}-\\d{2}:\\d{2}";

    private static String timePatternTwo = "\\d{2}日\\d{2}:\\d{2}-\\d{2}日\\d{2}:\\d{2}";

    private static String timePatternThree = "\\d{2}日\\d{2}-\\d{2}时";

    public static void main(String[] args) {
        String releaseTime = "2020年11月25日15时24分";
        // 25日22:00-26日04:00  26日02:00-07:00  25日18-21时
        String existTime = "26日02:00-07:00";
        AreaAndTimeBO.StartAndEndTimeBO timeBO = obtainTime(releaseTime, existTime);
        System.out.println(timeBO);

    }

    /**
     * 通过发布时间获取到开始时间和结束时间
     *
     * @param releaseTime 发布时间
     * @param existTime   出现时段
     */
    public static AreaAndTimeBO.StartAndEndTimeBO obtainTime(String releaseTime, String existTime) {
        String releaseTimeRep = releaseTime.replace("年", "-").replace("月", "-")
                .replace("日", " ").replace("时", ":").replace("分", ":00");
        existTime = existTime.replace("：", ":");
        AreaAndTimeBO.StartAndEndTimeBO timeBO = new AreaAndTimeBO.StartAndEndTimeBO();
        Date releaseDateTime = Java8DateUtils.parseDateTimeStr(releaseTimeRep, Java8DateUtils.DATE_TIME);
        timeBO.setReleaseTime(releaseDateTime);
        Calendar c = Calendar.getInstance();
        c.setTime(releaseDateTime);
        if (existTime.matches(timePatternTwo)) {
            String[] split = existTime.split("-");
            String startStr = split[0];
            String endStr = split[1];
            timeBO.setStartTime(setDayAndHourDate(startStr, c));
            timeBO.setEndTime(setDayAndHourDate(endStr, c));

        } else if (existTime.matches(timePatternThree) || existTime.matches(timePatternOne)) {
            String day = existTime.substring(0, existTime.indexOf("日"));
            String startHour = existTime.substring(existTime.indexOf("日") + 1, existTime.indexOf("日") + 3);
            String endHour = existTime.substring(existTime.indexOf("-") + 1, existTime.indexOf("-") + 3);
            c.set(Calendar.DAY_OF_MONTH, Integer.parseInt(day));
            c.set(Calendar.HOUR_OF_DAY, Integer.parseInt(startHour));
            c.set(Calendar.MINUTE, 0);
            timeBO.setStartTime(c.getTime());
            c.set(Calendar.HOUR_OF_DAY, Integer.parseInt(endHour));
            timeBO.setEndTime(c.getTime());
        }
        return timeBO;
    }

    /**
     * 获取date类型的时间
     *
     * @param time 26日22:00
     * @return
     */
    public static Date setDayAndHourDate(String time, Calendar c) {
        String day = time.substring(0, time.indexOf("日"));
        String hour = time.substring(time.indexOf("日") + 1, time.indexOf("日") + 3);
        c.set(Calendar.DAY_OF_MONTH, Integer.parseInt(day));
        c.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hour));
        c.set(Calendar.MINUTE, 0);
        return c.getTime();
    }
}
